#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
import time
import signal
import MySQLStress

THREAD_COUNT = 10

SHUTDOWN = False

def signal_handler(snum, sfrm) :
	global SHUTDOWN
	SHUTDOWN = True

signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT , signal_handler)
signal.signal(signal.SIGHUP , signal_handler)
signal.signal(signal.SIGPIPE, signal_handler)


def main(args) :

	type, thread_count, user_count, idx = args
	idx = int(idx)
	thread_count = int(thread_count)
	user_count = int(user_count)

	start_pos = idx * thread_count * user_count

	thread_list = list()

	for i in range(0, thread_count) :
		thread_list.append(MySQLStress.MySQLStress("172.16.3.31", type, user_count, i*user_count+start_pos))

	for th in thread_list :
		th.setDaemon(True)
		th.start()

	while (not SHUTDOWN) :
		time.sleep(1)

	for th in thread_list :
		th.shutdown = True

	for th in thread_list :
		th.join()
	
def usage() :
	print "usage : %s type thread_count user_count idx" % sys.argv[0]
	print "ex    : %s [item|mail|news] 10 50 [0|1|2|3|4..]" % sys.argv[0]
	sys.exit()

if __name__ == "__main__" :
	if (len(sys.argv) != 5) : usage()
	main(sys.argv[1:])
